package com.sx.core.mybatis;

import com.sx.core.page.Page;
import com.sx.core.page.PageImpl;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.dao.support.PersistenceExceptionTranslator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lihao on 2017/11/10.
 */
public class SessionTemplate extends SqlSessionTemplate {

    public SessionTemplate(SqlSessionFactory sqlSessionFactory) {
        super(sqlSessionFactory);
    }

    public SessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) {
        super(sqlSessionFactory, executorType);
    }

    public SessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) {
        super(sqlSessionFactory, executorType, exceptionTranslator);
    }
    private String statement(Class<?> clazz){
        return clazz.getName();
    }
    public  <T> Page<T> listPage(Class<T> clazz, String statement, int page, int limit,
                                 Object parameter) {
        statement=statement(clazz)+"."+statement;
        return listPage(statement,page,limit,parameter);
    }
    public  <T> Page<T> listPage(String statement, int page, int limit,
                                      Object parameter) {
        List<T> content=null;
        Long count=1l;
        count = selectOne(statement,
                new PaginationInterceptor.CountParameter(parameter));

        if (count.equals(0L)) {
            return new PageImpl<T>(new ArrayList<T>(), new Pagination(page, limit), count);
        }

        content = selectList(statement, parameter, new Pagination(page, limit));
        return new PageImpl<T>(content, new Pagination(page - 1, limit), count);
    }



    public <T> List<T> list(Class<T> clazz) {
        return list(clazz,"selectAll",null);
    }
    public <T> List<T> list(Class<T> clazz,String statement,Object parameter) {
        return selectList(statement(clazz)+"."+statement,parameter);
    }

    public <T> T get(Class<T> clazz,Object parameter) {
        return get(clazz,"selectByPrimaryKey",parameter);
    }
    public <T> T get(Class<T> clazz,String statement,Object parameter) {
        return selectOne(statement(clazz)+"."+statement,parameter);
    }


    public <T> int delete(Class<T> clazz,Object id) {
        return delete(clazz,"deleteByPrimaryKey",id);
    }
    public <T> int delete(Class<T> clazz,String statement,Object parameter) {
        return delete(statement(clazz)+"."+statement,parameter);
    }
    public <T> int save(T entity) {
        return save(entity.getClass(),"insertSelective",entity);
    }

    public <T> int save(Class<T> clazz,String statement,Object parameter) {
        return insert(statement(clazz)+"."+statement,parameter);
    }

    public <T> int edit(T entity) {
        return edit(entity.getClass(),"updateByPrimaryKeySelective",entity);
    }
    public <T> int edit(Class<T> clazz,String statement,Object parameter) {
        return update(statement(clazz)+"."+statement,parameter);
    }





}
